<?php
/**
 * Description
 * User: duanwenjie
 * Date: 2019/5/29
 * Time: 11:59 AM
 */

namespace app\index\service;
use app\common\ConfigBase;
use app\common\model\ToolModel;
use app\common\service\DownService;
use app\common\service\SkuService;
use think\Db;
use think\Exception;

class DrawpodetailService
{
    static public function getList($param, $is_download = false)
    {
        try{
            $where = "where 1 = 1 ";
            $pageNumber = isset($param['page']) ? $param['page'] : 1;
            $pageData = isset($param['limit']) ? $param['limit'] : 20;
            $po = isset($param['po']) ? $param['po'] : '';  //采购单单号
            $sku = isset($param['sku']) ? $param['sku'] : '';  //SKU
            $status = isset($param['status']) ? $param['status'] : '';  //状态
            $offset = ($pageNumber - 1) * $pageData;
            $limit = " LIMIT $offset,$pageData";

            if (!empty($po)) {
                $po = trim($po);
                $where .= " AND po = '{$po}'";
            }
            if (!empty($sku)) {
                $sku = trim($sku);
                $where .= " AND sku = '{$sku}'";
            }
            if (!empty($status)) {
                $where .= " AND status = {$status}";
            }

            $sqlCount = "SELECT COUNT(id) num FROM draw_po_detail {$where}";
            $countArr = ToolModel::queryData($sqlCount);
            if (empty($countArr) || $countArr[0]['num'] == 0) {
                if ($is_download) exitJson(["code" => "500", "msg" => "无有效数据"]);
                return array('total' => 0, 'list' => array());
            }
            $fileds = 'id,po,sku,sku_name,po_quantity,draw_quantity,surplus_quantity,
                      in_quantity,update_time,status,case status when 10 then "待采购" 
                      when 20 then "已打印" when 40 then "未完全到货" when 50 then "完全到货"
                      when 60 then "已质检" when 70 then "完全入库" when 90 then "手动完结" 
                      when 100 then "取消" end status_name';

            $sql = "SELECT {$fileds} FROM draw_po_detail {$where} ORDER BY id DESC {$limit}";
            if ($is_download) return "SELECT {$fileds} FROM draw_po_detail {$where} ORDER BY id DESC ";

            $list = ToolModel::queryData($sql);
            $data = array();
            $data['total'] = intval($countArr[0]['num']);
            $data['list'] = $list;

            return $data;
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 导出数据
     * @param $param
     * @return array
     * @author duanwenjie
     */
    static public function export($param)
    {
        $sql = self::getList($param,true);
        $title = array(
            'po' => '采购单单号',
            'sku' => 'SKU',
            'sku_name' => 'SKU中文名称',
            'po_quantity' => '采购数量',
            'draw_quantity' => '已提数量',
            'surplus_quantity' => '提货结余',
            'in_quantity' => '入库数量',
            'status_name' => '状态'
        );

        $res = DownService::getData($sql,$title,'',$param);

        return $res;
    }

}